<?
/************code by seven****************/
require("../../common/init.php");
require("author.php");
require("upfile.php");

$gee_cate = $product_category;
$gee_product = $product;
$gee = $tag_list;

$url='tags_list';
$s=isset($_GET["s"])?clean($_GET["s"]):''; // search/*
$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 操作: 查看/编辑/删除
$action=isset($_POST["action"])?clean($_POST["action"]):''; // 数据库操作: CURD
$id=0; if (isset($_GET["id"])) $id = clean($_GET["id"]);
$filter="0";
if(isset($_GET["filter"]))$filter=clean($_GET["filter"]);
if(isset($_POST["filter"]))$filter=clean($_POST["filter"]);


//pre($tag_name);
//$cate = $gee_cate->get_row($promotion_id, 'promotion_id');
//$product = $gee_product->get_row($promotion_id, 'promotion_id');

// build category tree 构建分类级别树
$tem = $gee_cate->get_rows(1024,'','','seq_no');
$tree=unlimitedlevel($tem);

$cate_array=array();
foreach($tree as $val){
    $cate_array[$val['id']]=array(
        'id'=>$val['id'],
        'name'=>$val['html_prefix'].$val['name_'.$lang],
    );
}

switch ($filter){
    case 0:
        $title = ROOT_TAGS_LIST;
        break;
    case 1:
        $title=ROOT_SALES_PROMOTION_UNFINISH;
        break;
    case 2:
        $title = ROOT_SALES_PROMOTION_FINISH;
        break;
    default:
        $title=ROOT_SALES_PROMOTION;
}

// sql action / sql 动作
switch ($action){
    case "update":
        sql_update();
        break;
    case "add":
        sql_insert();
        break;
    case "deletes":
        sql_delete();
        break;
}
if($id==0){
    $page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"]))$page=$_GET["page"];
    $order='id';
    $where = array();
    $info = array();
    $where_tags = array();
    $tags_id_arr = array();
    $where_d = array();

    if($s!='') {
        $where_tags[] = array('name'=>1, 'val'=>1, 'opn'=>'=', 'and'=>'and', 'start'=>'(');
        $where[]=array('name'=>'name_'.$lang,'val'=>$s,'opn'=>'like', 'and'=>'and');
        $tags_info = $tags->get_rows_advanced(1024, $where);
        foreach($tags_info as $val){
            $tags_id_arr[] = $val['id'];
        }
        $where_tags[] = array('name'=>'tag_id', 'val'=>$tags_id_arr, 'opn'=>'in', 'and'=>'and');

        foreach($code_array['tags_table'] as $val_code){
            $p_id_arr = array();
            $table_name = $val_code['name_'.$lang];
            if($table_name != 'product'){
                $where_d[]=array('name'=>'name','val'=>$s,'opn'=>'like', 'and'=>'and');
                $p_info = $$table_name->get_rows_advanced(1024, $where_d);
                foreach($p_info as $val_d){
                    $p_id_arr[] = $val_d['id'];
                }
//                $where_tags[] = array('name'=>'p_id', 'val'=>$p_id_arr, 'opn'=>'in', 'and'=>'or');
                $where_tags[] = array('name'=>'p_id', 'val'=>$p_id_arr, 'opn'=>'in', 'and'=>'or', 'start'=>'(');
                $where_tags[] = array('name'=>'tags_table', 'val'=>$val_code['id'], 'opn'=>'=', 'and'=>'and','end'=>')');
            }else{
                $p_info = $$table_name->get_rows_advanced(1024, $where);
                foreach($p_info as $val_p){
                    $p_id_arr[] = $val_p['id'];
                }
                $where_tags[] = array('name'=>'p_id', 'val'=>$p_id_arr, 'opn'=>'in', 'and'=>'or', 'start'=>'(');
                $where_tags[] = array('name'=>'tags_table', 'val'=>$val_code['id'], 'opn'=>'=', 'and'=>'and','end'=>')');
            }
            unset($p_id_arr);
        }
        $where_tags[] = array('name'=>1, 'val'=>1, 'opn'=>'=', 'and'=>'and', 'end'=>')');

    }

    $pages=$gee->count_pages($where_tags);
    $info=$gee->get_page($page,$where_tags,$order);
//    }
    for($i = 0; $i < count($info); $i++){
        $table = $code_array['tags_table'][$info[$i]['tags_table']]['name_'.$lang];
        $source = $code_array['tags_table_description'][$info[$i]['tags_table']]['name_'.$lang];
        $tag_info = $tags->get_row($info[$i]['tag_id']);
        $info[$i]['tag_name'] = $tag_info['name_'.$lang];
        $list_name = $$table->get_row($info[$i]['p_id']);
        if($table == 'product'){
            if(!empty($list_name['sku'])){
                $info[$i]['list_name'] = $list_name['name_'.$lang].' / '.$list_name['sku'];
            }else{
                $info[$i]['list_name'] = $list_name['name_'.$lang];
            }
            $info[$i]['list_table'] = $source;
        }else{
            $info[$i]['list_name'] = $list_name['name'];
            $info[$i]['list_table'] = $source;
        }
    }
}
else $info=$gee->get_row($id);

function add_cate(){
    global $gee_cate, $url, $id;
    $id = $_POST['id'];
    $discount_type = 1;
    $discount_value = 20;
    $promotion_id = 9;
    $arr=array(
        'discount_type'=>$discount_type,
        'promotion_id' => $promotion_id,
        'discount_value' => $discount_value,
    );
    //pre($arr); exit;
    $gee_cate->update($arr, 'id', $id );
    echo $discount_value;
    exit;
    // wheel("$url.php?promotion_id=".$promotion_id);
}
function add()
{
    global $h, $url, $info, $cate_info, $tag_id, $tag_type;
    $row = array();
    echo $h->form($url.'.php', 1, '');
    // if($tag_type == 1){
    show_box($row, true, true);
    // }else{
    //    show_document($cate_info, true, true);
    //}
    echo $h->hidden('action', 'add');
    echo $h->former();
}
function update($info)
{
    global $gee, $h, $url, $id;
    if($id=="")
    {
        echo "Invali argument!!!";
        exit;
    }
    echo $h->form($url.'.php', 1, '');
    show_box($info, true, false);
    echo $h->hidden('action', 'update');
    echo $h->former();
}
function delete($id)
{
    global $gee,$url,$tags,$id,$tag_id,$tag_type;
    pre($id);
    if($gee->delete($id)){
        $tag_arr = $tags->get_row($tag_id);
        $arr = array('count' => ($tag_arr['count'] - 1));
        $tags->update($arr, 'id', $tag_id);
        wheel("$url.php?tag_id=".$tag_id);
    }
}
function show_info($info)
{
    global $id;
    if($id==0)
    {
        show_boxes($info);
    }
    else
    {
        show_box($info, false);
    }
}
function sql_insert()
{
    global $gee,$url,$lang;
    $arr=array(
        'full'=>$_POST["full"],
        'promotion_id'=>$_POST["promotion_id"],
        'cut' => $_POST['cut'],
        'type' => $_POST['type'],
        'user_id'=>decrypt($_COOKIE['userid']),
    );
    //pre($arr); exit;
    if($gee->insert($arr))
        yell(ROOT_ADD_SUCCESS,$url.'.php');
}
function sql_update()
{
    global $gee, $url,$lang;
    $id=$_POST["id"];
    $arr=array(
        'full'=>$_POST["full"],
        'promotion_id' => $_POST['promotion_id'],
        'cut' => $_POST['cut'],
        'type' => $_POST['type'],
        'user_id'=>decrypt($_COOKIE['userid']),
    );
    //pre($arr); exit;
    $gee->update($arr, 'id', $id );
    yell(ROOT_UPDATE_SUCCESS,$url.'.php?tag_id='.$_POST['tag_id']);

}

function sql_delete()
{
    global $gee,$page,$url;
    $page=iso(@$_POST["page"]);
    $s=iso(@$_POST["s"]);
    $filter=iso(@$_POST["filter"]);
    $all=$_POST["all"];
    $arrdelete=array();
    if(count($all)>0)foreach($all as $a){ $arrdelete[]=$a; }
    if($gee->delete_rows($arrdelete))
        wheel($url.'.php?filter='.$filter.'&s='.$s.'&page='.$page);
}

// view 加载视图
require ROOTVIEW.$url.'.php';

// end
